package com.itheima.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;

import com.itheima.entity.Result;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * （1）使用spring security获取当前登录用户
 *
 * （2）根据RBAC权限模型查询当前用户关联的菜单
 *
 * （3）主页左侧菜单栏动态显示上一步查询出的菜单
 */
@Controller
public class SecurityController {
    @Reference
    private MenuService menuService;

    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public Result currentUserName(Principal principal) {
        try {
            String name = principal.getName();
            //根据当前用户名获取
            List<Map> list =  menuService.findAllByUsername(name);
            return new Result(true,"获取当前用户菜单列表成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取当前用户菜单列表失败");
        }
    }
}